// pages/ScenicArea/ScenicAreaDetail/index.js
const common = require('../../../public.js')
const config = require('../../../config')
const utils = require('../utils.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    storeInfo: {},
    moduleType: '', //模块类型 1 = 景区景点  2 = 文化场馆   后加的 3 = 运动健身
    currentPrebuy: {}, // 点击购买须知存当前对象
    currentBanner: 1,
    ticketShowNum: 4,
    showMustKnow: false,
    showCouponsPanel: false,
    articleList: [], // 只有 moduleType=2 有数据
    isLoadingBusy: false,
    ticketName: '',
    isFyApp: config.isFyApp,
    active: 0,
    name: "",
    type: ""
  },

  onLoad: function (options) {
    console.log(options);
    this.setData({
      name: options.name,
      type: options.type
    })
    if (options.q) {
      let scan_url = decodeURIComponent(options.q);
      console.log("scan_url:" + scan_url)
      let video_id = scan_url.match(/\d+/) //提取链接中的数字，也就是链接中的参数id，/\d+/ 为正则表达式
      console.log(video_id[0])
      this.setData({
        isLoadingBusy: true,
        storeId: video_id[0],

      }, _ => {
        // this.getStoreInfo()
        this.getInfo()
      })
      return
    }
    const that = this
    //获得胶囊位置
    if (this.data.isFyApp) {
      // 飞羊小程序显示的是页面，展示返回按钮
      const capsule = wx.getMenuButtonBoundingClientRect()
      this.setData({
        topOffset: capsule.top,
        height: capsule.height
      })
    }
    that.setData({
      isLoadingBusy: true,
      storeId: options.id ? options.id : config.id,
      // storeId: 4133,// 2439 没数据  4133 有数据   415测试小店专用
      moduleType: options.moduleType ? options.moduleType : config.type
    }, _ => {
      //   this.data.name=='运动健身' ? that.getInfo() :   that.getStoreInfo()
    })
  },
  onShow() {
    this.getInfo()
  },
  getInfo() {
    const that = this
    const tempJson = {
      // storeId: that.data.storeId
      storeId:91
    }
    common.post('/store/getVenueMainPageInfo', tempJson,
      function (res) {
        if (res.status === 200) {
          that.setData({
            storeInfo: res.data,
            isFyApp: false
          })
          if (that.data.moduleType == 3) {
            that.setData({
              articleList: res.data.articleList
            })
          }
        } else {
          common.showRequestErrMsg(res)
        }
      },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      })
  },
  //   getStoreInfo() { // 初始化店铺信息
  //     const that = this
  //     const tempJson = {
  //       storeId: that.data.storeId
  //     // storeId:91
  //     }
  //     common.post('/store/mainPageInfo', tempJson,
  //       function (res) {
  //           console.log(res);
  //         if (res.status === 200) {
  //           that.setData({
  //             storeInfo: res.data,
  //           })
  //           if (that.data.moduleType == 2) { // 文化场馆
  //             that.setData({
  //               articleList: res.data.articleList
  //             })
  //           }
  //         } else {
  //           common.showRequestErrMsg(res)
  //         }
  //       },
  //       function () {
  //         that.setData({
  //           isLoadingBusy: false
  //         })
  //       })
  //   },
  toArticle(e) { // 跳转文章详情
    let {
      item
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/discovery/articleDetail/index?id=' + item.id,
    })
  },
  toBookPage(e) { // 立即预订
    let {
      item
    } = e.currentTarget.dataset
  
    wx.navigateTo({
      url: '/pages/ScenicArea/ScenicAreaChooseTickets/index?ticketId=' + item.id +
        '&storeId=' + this.data.storeId +
        '&ticketName=' + item.ticketName +
        '&storeName=' + this.data.storeInfo.store.name
        // '&venuName=' +  item.name
    })
  },
  toReservationPage(e) { // 立即预订
    let {
      item
    } = e.currentTarget.dataset
    console.log('参数为', '/pages/ScenicArea/ScenicAreaChooseTickets/index?ticketId=' + item.id +
      '&storeId=' + this.data.storeId +
      '&ticketName=' + item.ticketName +
      '&storeName=' + this.data.storeInfo.store.name);
    wx.navigateTo({
      url: '../../../packageA/pages/Goods/reservation/index?venueId=' + item.id +
        '&storeId=' + this.data.storeId +
        '&name=' + item.name +
        '&storeName=' + this.data.storeInfo.store.name
    })
  },
  toGoodsDetail(e) { // 购买景区项目
    const item = e.currentTarget.dataset.item
    common.gotoGoodsPage(item)
  },
  toAlbum() { // 进入商家相册
    let backUpPicList = encodeURIComponent(JSON.stringify(this.data.storeInfo.backUpPicList))
    let backUpVideoList = encodeURIComponent(JSON.stringify(this.data.storeInfo.backUpVideoList))
    wx.navigateTo({
      url: '/pages/ScenicArea/ScenicAreaPhotoAlbum/index?backUpPicList=' + backUpPicList + '&backUpVideoList=' + backUpVideoList,
    })
  },
  mustKnow(e) { // 购买须知弹框
    let {
      item
    } = e.currentTarget.dataset
    this.setData({
      showMustKnow: true,
      currentPrebuy: item,
      ticketName: item.ticketName
    })
  },
  closeMustKnow() { // 关闭购买须知弹框
    this.setData({
      showMustKnow: false
    })
  },
  openCouponsPanel() { // 打开优惠券弹框
    this.setData({
      showCouponsPanel: true
    })
  },

  closeCouponsPanel() { // 关闭优惠券弹框
    this.setData({
      showCouponsPanel: false
    })
  },
  bannerChange(e) { // 轮播滚动触发
    let {
      current
    } = e.detail
    this.setData({
      currentBanner: current + 1
    })
  },
  onChangeTabbar(e) { //切换底部导航
    console.log(e)
    const index = e.detail.index
    const url = e.detail.url
    if (index != this.data.active) {
      utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
    }
  },
  showStoreIntro() {
    this.storeIntroduce(0)
  },
  storeIntroduce(type = 0) {
    wx.navigateTo({
      url: '/pages/ScenicArea/ScenicAreaIntro/index?storeId=' + this.data.storeId
        + '&type=' + type
        + '&moduleType=' + this.data.moduleType
        + '&title=' + this.data.storeInfo.store.name
    })
  },
  mapGuide() {
    console.log(this.data.storeInfo.store.address)
    if (this.data.storeInfo.store.ypoint && this.data.storeInfo.store.xpoint) {
      wx.openLocation({
        latitude: Number(this.data.storeInfo.store.ypoint),
        longitude: Number(this.data.storeInfo.store.xpoint),
        name: this.data.storeInfo.store.name ? this.data.storeInfo.store.name : null,
        address: this.data.storeInfo.store.address ? this.data.storeInfo.store.address : null,
        scale: 16
      })
    } else {
      common.showToast('未获取到位置信息')
    }
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  showMore() { // 查看更多
    if (this.data.ticketShowNum == 4) {
      this.setData({
        ticketShowNum: this.data.storeInfo.ticketList.length
      })
    } else {
      this.setData({
        ticketShowNum: 4
      })
    }
  },
  callPhone(e) {
    let mobile = e.currentTarget.dataset.mobile
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  onShareAppMessage: function () {

  }
})